<?php
namespace app\index\controller;

use think\Controller;

class Login extends Controller
{
    public function login()
    {
        if (!session('username')){
            return $this->fetch();
        }
        $this->redirect('/');
    }

    public function doLogin()
    {
        $username = request()->param('username');
        $password = request()->param('password');
        $user = db('user')->where('username' , $username)->find();

        //session('username' , '2133213');
       if (md5($password) == $user['password']){
           session('username' , $username);
           cookie('username' , $username);
           echo json_encode([
               'code' => 0,
           ]);
       } else{
           echo json_encode([
               'code' => 1,
               'msg' => '密码错误'
           ]);
       }
    }

    public function doLogout()
    {
        session(null);
        echo json_encode([
            'code' => 0
        ]);
    }
}